<?php
/*
 |--------------------------------------------------------------------------
 | SkyCaiji (蓝天采集器)
 |--------------------------------------------------------------------------
 | Copyright (c) 2018 http://www.skycaiji.com All rights reserved.
 |--------------------------------------------------------------------------
 | 使用协议  http://www.skycaiji.com/licenses
 |--------------------------------------------------------------------------
 */

 namespace Admin\Event; use Think\Controller; use Admin\Model\CollectedModel; use Think\Db; if(!defined('IN_SKYCAIJI')) { exit('NOT IN SKYCAIJI'); } class RdbEvent extends ReleaseEvent { public function setConfig($config){ $db=I('db/a','','trim'); foreach ($db as $k=>$v){ if(empty($v)&&'pwd'!=$k){ $this->error(L('error_null_input',array('str'=>L('rele_db_'.$k)))); } } $config['db']=$db; $config['db_table']=I('db_table/a','','trim'); foreach($config['db_table']['field'] as $tbName=>$tbFields){ foreach ($tbFields as $tbField=>$fieldVal){ if(empty($fieldVal)){ unset($config['db_table']['field'][$tbName][$tbField]); unset($config['db_table']['custom'][$tbName][$tbField]); continue; } } } return $config; } public function export($collFieldsList,$options=null){ $db_config=$this->get_db_config($this->config['db']); $dbClass=M('',null,$db_config); $addedNum=0; $dbCharset=strtolower($db_config['db_charset']); if(empty($dbCharset)||$dbCharset=='utf-8'||$dbCharset=='utf8'){ $dbCharset=null; } foreach ($collFieldsList as $collFields){ $dbClass->startTrans(); $contUrl=$collFields['url']; $collFields=$collFields['fields']; $tableFields=array(); foreach ($this->config['db_table']['field'] as $tbName=>$tbFields){ foreach ($tbFields as $tbField=>$fieldVal){ if(empty($fieldVal)){ unset($tbFields[$tbField]); continue; } if(strcasecmp('custom:',$fieldVal)==0){ $fieldVal=$this->config['db_table']['custom'][$tbName][$tbField]; }elseif(preg_match('/^field\:(.+)$/ui',$fieldVal,$collField)){ $fieldVal=$this->get_field_val($collFields[$collField[1]]); $fieldVal=is_null($fieldVal)?'':$fieldVal; } if(!empty($dbCharset)){ $fieldVal=$this->utf8_to_charset($dbCharset, $fieldVal); } $tbFields[$tbField]=$fieldVal; } $tableFields[$tbName]=$tbFields; } if(!empty($tableFields)){ $errorMsg=false; $autoidList=array(); foreach ($tableFields as $table=>$fields){ foreach ($fields as $k=>$v){ if(preg_match('/^auto_id\@([^\s]+)$/i', $v,$autoidTbName)){ $autoidTbName=trim($autoidTbName[1]); $fields[$k]=$autoidList[$autoidTbName]; } } try { $autoidList[$table]=$dbClass->table($table)->add($fields); }catch (\Exception $ex){ $errorMsg=$ex->getMessage(); $this->echo_msg($ex->getMessage()); break; } if($autoidList[$table]<=0){ break; } } $returnData=array('id'=>0); if(!empty($errorMsg)){ $dbClass->rollback(); $returnData['error']=$errorMsg; }else{ $dbClass->commit(); reset($autoidList); list($firstTable,$firstId) = each($autoidList); $firstId=intval($firstId); if($firstId>0){ $addedNum++; $returnData['id']=$firstId; $returnData['target']="db:{$db_config['db_name']}@table:{$firstTable}@id:{$firstId}"; }else{ $returnData['error']='数据插入失败'; } } $this->record_collected($contUrl,$returnData,$this->release); } } $this->echo_msg('成功发布'.$addedNum.'条数据','green'); return $addedNum; } public function get_db_config($config_db){ $db_config=array( 'db_type' => $config_db['type'], 'db_user' => $config_db['user'], 'db_pwd' => $config_db['pwd'], 'db_host' => $config_db['host'], 'db_port' => $config_db['port'], 'db_charset' => $config_db['charset'], 'db_name' => $config_db['name'] ); if(strcasecmp($db_config['db_charset'], 'utf-8')===0){ $db_config['db_charset']='utf8'; } return $db_config; } } ?>